﻿using DataAccess.DAL;
using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class AlimamaController : BaseController
    {
        public ActionResult Index()
        {
            
            return View( );
        }

        [HttpPost]
        public JsonResult GetAlimamas()
        {
            AlimamaDAL alimamaDAL = new AlimamaDAL();

            string keyword = System.Web.HttpContext.Current.Request.Form["keyword"];
            int limit = XYD.Common.Request.GetFormInt("limit", 10);
            int offset = XYD.Common.Request.GetFormInt("offset", 1);
            int state = XYD.Common.Request.GetFormInt("state", -1);

            string strWhere = " promoter_id=@promoter_id ";
            string orderbys = "";

            if (state != -1)
            {
                strWhere += " and state=@state" ;
            }

            if (!string.IsNullOrEmpty(keyword))
            {
                strWhere +=  " and username like @keyword " ;
            }
            int count = 0;

            List<Alimama> alimamas = alimamaDAL.GetAlimamas(strWhere, orderbys, limit, (offset / limit) + 1, out count, new { keyword = "'%"+keyword+"%'",state = state, promoter_id = UserSession.user_id });
              
            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            result.Datas = new PageData<Alimama> { Total = count, Rows = alimamas, PageIndex = (offset / limit) + 1, PageSize = limit };

            return Json(result);  
        }

        public ActionResult Edit(int id = 0)
        {
            AlimamaDAL alimamaDAL = new AlimamaDAL();

            Alimama alimama;
            if (id != 0)
            {
                alimama = alimamaDAL.Get(id);
            }
            else
            {
                alimama = new Alimama();
            }

            return View(alimama);
        }

        [HttpPost]
        public JsonResult Edited()
        { 
            dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(XYD.Common.Utils.UrlDecode(Request.Form.ToString()));

            AlimamaDAL alimamaDAL = new AlimamaDAL();

            Alimama alimama = new Alimama();

            if (int.Parse(obj.id.Value) != 0)
            {
                alimama = alimamaDAL.Get(int.Parse(obj.id.Value));

                alimama.state = int.Parse(obj.state.Value);
                alimama.username = obj.username.Value;
                alimama.password = obj.password.Value;
                alimama.remark = obj.remark.Value;

                bool boo = alimamaDAL.Update(alimama);

                return Json(new Result { State = boo, Message = boo ? "更新成功" : "更新失败" });
            }
            else
            {
                if (alimamaDAL.Exist(obj.username.Value))
                {
                    return Json(new Result { State = false, Message = "添加失败，已存在相同账号" });
                }

                alimama.state = int.Parse(obj.state.Value);
                alimama.username = obj.username.Value;
                alimama.password =obj.password.Value;
                alimama.synchro = DateTime.Now;
                alimama.remark = obj.remark.Value;

                alimama.add_data = DateTime.Now;
                alimama.promoter_id = UserSession.user_id;
                alimama.is_delete = 0;

                bool boo = alimamaDAL.Insert(alimama);

                return Json(new Result { State = boo, Message = boo ? "添加成功" : "添加失败" });
            }
        }

        
    }
}